package com.example.myaccount.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myaccount.R;

import java.util.ArrayList;

import com.example.myaccount.activity.AddFlowActivity;
import com.example.myaccount.activity.MainActivity;
import com.example.myaccount.component.FlowDialog;
import com.example.myaccount.constant.MyConst;
import com.example.myaccount.database.DatabaseHelper;
import com.example.myaccount.util.ResourceUtil;


public class AddOutcomeFragment extends Fragment implements View.OnClickListener{

    private DatabaseHelper databaseHelper;
    private View root;
    private SharedPreferences sp;
    private ArrayList<String> typesList;
    private ArrayList<String> typesIdList;
    private int typeNum,checkedId,colorBlue,colorGrey;
    private FlowDialog flowDialog;


    private ArrayList<LinearLayout> mLlList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_add_outcome, container, false);

        initData();
        initView();

        return root;

    }

    private void initData() {
        databaseHelper = DatabaseHelper.getDefaultDatabaseHelper(getContext());

        sp = getActivity().getSharedPreferences(MyConst.SETTINGS, Context.MODE_PRIVATE);
        if(sp.getInt(MyConst.SETTINGS_TYPES_OUTCOME_NUM,0)==0){
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt(MyConst.SETTINGS_TYPES_OUTCOME_NUM,MyConst.SETTINGS_TYPES_OUTCOME_NUM_DEFAULT);
            String temp = getString(R.string.type_daily_necessities)
                    +"-"+getString(R.string.type_meal)
                    +"-"+getString(R.string.type_clothes)
                    +"-"+getString(R.string.type_transportation)
                    +"-"+getString(R.string.type_house)
                    +"-"+getString(R.string.type_medicine)
                    +"-"+getString(R.string.type_drinking)
                    +"-"+getString(R.string.type_fruit)
                    +"-"+getString(R.string.type_snack)
                    +"-"+getString(R.string.type_makeup)
                    +"-"+getString(R.string.type_movie)
                    +"-"+getString(R.string.type_book)
                    +"-"+getString(R.string.type_pet)
                    +"-"+getString(R.string.type_redpacket)
                    +"-"+getString(R.string.type_gift)
                    +"-"+getString(R.string.type_vacation)
                    +"-"+getString(R.string.type_fix)
                    +"-"+getString(R.string.type_game)
                    +"-"+getString(R.string.type_car)
                    +"-"+getString(R.string.type_education)
                    +"-"+getString(R.string.type_sports)
                    +"-"+getString(R.string.type_digitalproduct)
                    +"-"+getString(R.string.type_child)
                    +"-"+getString(R.string.type_other)
                    +"-"+getString(R.string.type_edit);
            editor.putString(MyConst.SETTINGS_TYPES_OUTCOME,temp);
            editor.putString(MyConst.SETTINGS_TYPES_OUTCOME_ID,MyConst.SETTINGS_TYPES_OUTCOME_ID_DEFAULT);
            editor.commit();
        }

        typeNum = sp.getInt(MyConst.SETTINGS_TYPES_OUTCOME_NUM,0);

        typesList = new ArrayList<>();
        String[] temp = sp.getString(MyConst.SETTINGS_TYPES_OUTCOME,"").split("-");
        for(String type:temp){
            typesList.add(type);
        }

        typesIdList = new ArrayList<>();
        temp = sp.getString(MyConst.SETTINGS_TYPES_OUTCOME_ID,"").split("-");
        for(String typeId:temp){
            typesIdList.add(typeId);
        }

        colorBlue = getResources().getColor(R.color.text_color_blue);
        colorGrey = getResources().getColor(R.color.text_color_grey_dark);

        mLlList = new ArrayList<>();
    }

    private void initView() {
        // TODO: 2021/12/1 动态添加视图
//        GridLayout gridLayout = root.findViewById(R.id.gridlayout);
//        Context context = AddFlowActivity.getContext();
//        LinearLayout mLl = new LinearLayout(context);
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//        ImageView mIv = new ImageView(context);
//        TextView mTv = new TextView(context);
//
//        mLl.setOrientation(LinearLayout.VERTICAL);
//        mLl.setId();
//
//        for (int i = 0; i < 6; i++) {
//            for (int j = 0; j < 3; j++) {
//                TextView textView = new TextView(mContext);
//                textView.setBackgroundResource(R.drawable.bg_blue_to_cc);
//                textView.setTextColor(mContext.getResources().getColorStateList(R.color.text_color_blue_to_cc));
//                //给TextView添加一个drawable left的icon
//                Drawable drawable = getResources().getDrawable(R.drawable.bg_check_enable_disable);
//                //下面这行必须，要不然图标不会显示
//                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                textView.setCompoundDrawables(drawable, null, null, null);
//                textView.setCompoundDrawablePadding(TO.dp2px(mContext, 7f));
//                //  TO.dp2px() 是个自定义方法可以用TypedValue.applyDimension()代替
//                textView.setPadding(TO.dp2px(mContext, 15f), 0, 0, 0);
//                textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
//                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
//                textView.setText("嘻哈");
//                //设置它的行 和 权重 有了权重才能水平均匀分布
//                //由于方法重载，注意这个地方的1.0f 必须是float，
//                GridLayout.Spec rowSpec = GridLayout.spec(i, 1.0f);
//                GridLayout.Spec columnSpec = GridLayout.spec(j, 1.0f);
//                GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, columnSpec);
//                //左边的靠左，右边的靠右，中间的居中，默认居中
//                switch (j) {
//                    case 0:
//                        params.setGravity(Gravity.LEFT);
//                        break;
//                    case 1:
//                        params.setGravity(Gravity.CENTER);
//                        break;
//                    case 2:
//                        params.setGravity(Gravity.RIGHT);
//                        break;
//                    default:
//                        params.setGravity(Gravity.CENTER);
//                        break;
//                }
//                params.bottomMargin = TO.dp2px(mContext, 11.5f);
//                //控制TextView的高度和宽度
//                params.height = TO.dp2px(mContext, 25.0f);
//                params.width = TO.dp2px(mContext, 90.0f);
//                gridLayout.addView(textView, params);
//            }
//        }
        LinearLayout mLl;
        int id;
        for(int i=0;i<typeNum;i++){
//            Log.d(MyConst.TAG,"ll_"+typesIdList.get(i));
            id = ResourceUtil.getResId("ll_"+typesIdList.get(i),R.id.class);
//            Log.d(MyConst.TAG,"id:"+id);
            mLl = root.findViewById(id);
            mLl.setOnClickListener(this);
//            mLlList.add(mLl);
        }
        flowDialog = new FlowDialog(root.getContext(), R.style.BottomDialog, new FlowDialog.PriorityListener() {
            @Override
            public void refreshPriorityUI(Double amount) {
//                DateFormat df = new SimpleDateFormat("yyyy_MM_dd");
//                String date = df.format(Calendar.getInstance().getTime());
                String date = flowDialog.getDate();
                String annotation = flowDialog.getAnnotation();
                amount = Math.abs(amount)*-1;
                Log.d(MyConst.TAG,typesIdList.get(checkedId)+" "+date+" "+amount+" "+annotation);
                if(sp.getString(MyConst.SETTINGS_STATE,"").equals(MyConst.SETTINGS_STATE_ADD)){
                    databaseHelper.insert(typesIdList.get(checkedId),date,amount,annotation);
                }else if(sp.getString(MyConst.SETTINGS_STATE,"").equals(MyConst.SETTINGS_STATE_UPDATE)){
                    int flowId = sp.getInt(MyConst.SETTINGS_FLOW_ID,0);
                    databaseHelper.updateFlow(flowId,typesIdList.get(checkedId),date,amount,annotation);
                }
                Intent intent = new Intent(AddFlowActivity.getActivity(),MainActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onClick(View v) {
        ImageView mIv;
        TextView mTv;
        int imageId;
        for(int i=0;i<typeNum;i++){
            if(v.getId()==ResourceUtil.getResId("ll_"+typesIdList.get(i),R.id.class)){
                mIv = root.findViewById(ResourceUtil.getResId("iv_"+typesIdList.get(i),R.id.class));
                mTv = root.findViewById(ResourceUtil.getResId("tv_"+typesIdList.get(i),R.id.class));
                if(mTv.getCurrentTextColor()==colorBlue){
                    imageId = ResourceUtil.getResId("ic_"+typesIdList.get(i),R.drawable.class);
                    mIv.setImageResource(imageId);
                    mTv.setTextColor(colorGrey);
                }else{
                    imageId = ResourceUtil.getResId("ic_"+typesIdList.get(i)+"_checked",R.drawable.class);
                    mIv.setImageResource(imageId);
                    mTv.setTextColor(colorBlue);
                }
                mIv = root.findViewById(ResourceUtil.getResId("iv_"+typesIdList.get(checkedId),R.id.class));
                mTv = root.findViewById(ResourceUtil.getResId("tv_"+typesIdList.get(checkedId),R.id.class));
                imageId = ResourceUtil.getResId("ic_"+typesIdList.get(checkedId),R.drawable.class);
                mIv.setImageResource(imageId);
                mTv.setTextColor(colorGrey);
                checkedId=i;
                flowDialog.show();
                return;
            }
        }
    }
}
